/**
  ******************************************************************************
  * @file                key_app.c
  * @author              BruceOu
  * @rtt version         V4.0.3
  * @version             V1.0
  * @date                2021-04-06
  * @blog                https://blog.bruceou.cn/
  * @Official Accounts   嵌入式实验楼
  * @brief
  ******************************************************************************
  */
#include "key_app.h"
#include "lcd.h"

uint32_t pulse_pulse;

#define BUTTON_PIN_0 rt_pin_get("PF.0")
#define BUTTON_PIN_1 rt_pin_get("PF.1")

static struct button btn_0;
static struct button btn_1;

uint32_t cnt_channels = 1;
uint32_t cnt_volume = 1;

uint32_t cnt_music = 0;

uint32_t  start_flag = 1;

uint8_t num_of_music = 0;

char *table[MAXNUM_OF_SONGS] =
{
    "/WuMingZhiBei.wav   ",
    "/DangZhenJiuHao.wav ",
    "/AiBuHuiJueJi.wav   ",
    "/KeXiMeiYouRuGuo.wav",
    "/ShiDaoRuJin.wav    "
//    "/                    ",
//    "/                    ",
//    "/                    ",
//    "/                    ",
//    "/                    ",
//    "/                   ",
//    "/                    ",
//    "/                    "
};

uint8_t space_num[MAXNUM_OF_SONGS]={4, 2, 4, 1, 5};

static uint8_t button_read_pin_0(void)
{
    return rt_pin_read(BUTTON_PIN_0);
}

static uint8_t button_read_pin_1(void)
{
    return rt_pin_read(BUTTON_PIN_1);
}

static void button_0_callback(void *btn)
{
    uint32_t btn_event_val;

    btn_event_val = get_button_event((struct button *)btn);

    switch(btn_event_val)
    {
    case SINGLE_CLICK:
        if (cnt_volume < 11 )
        {
            if(start_flag)
            {
                start_flag = 0;
                cnt_volume = (int)saia_volume_get()/10;
                pulse_pulse = 9000;
            }
            else
            {
                saia_volume_set(cnt_volume * 10);
                pulse_pulse = cnt_volume*9000;
            }
        }
        else
        {
            saia_volume_set(10);
            cnt_volume = 1;
            rt_kprintf("The volume has been adjusted to maximum\n");
        }
        cnt_volume ++;
        rt_kprintf("vol=%d\n", saia_volume_get());
        rt_kprintf("button 0 single click\n");
    break;

    case DOUBLE_CLICK:
        if (cnt_channels < 3)
        {
            saia_channels_set(cnt_channels);
        }
        else
        {
            saia_channels_set(cnt_channels);
            cnt_channels = 1;
        }
        cnt_channels++;
        rt_kprintf("button 0 double click\n");
    break;

    case LONG_PRESS_START:
        rt_kprintf("button 0 long press start\n");
    break;

    case LONG_PRESS_HOLD:
        rt_kprintf("button 0 long press hold\n");
    break;
    }
}

static void button_1_callback(void *btn)
{
    uint32_t btn_event_val;
    static uint8_t i=0;
    uint8_t j;

    btn_event_val = get_button_event((struct button *)btn);

    switch(btn_event_val)
    {
    case SINGLE_CLICK:
        wavplayer_play(table[(cnt_music) % NUM_OF_SONGS]);

        LCD_ShowString(0,250,32,"                    ",0);
        LCD_ShowString(space_num[(cnt_music) % NUM_OF_SONGS]*8,250,32,table[(cnt_music++) % NUM_OF_SONGS]+1,0);
        gui_circle(160, 350,RED,45,0);
        LCD_ShowString(128,330,32,"PLAY",0);
//        rt_kprintf("button 1 single click %d  %d  %s\n",cnt_music, num_of_music,table[(cnt_music) % num_of_music]);
    break;

    case DOUBLE_CLICK:
        if(i == 0)
        {
            wavplayer_pause();  //暂停
            i++;
            gui_circle(160, 350,RED,45,0);
            LCD_ShowString(128,330,32,"STOP",0);
        }
        else {
            wavplayer_resume(); //播放
            i=0;
            gui_circle(160, 350,RED,45,0);
            LCD_ShowString(128,330,32,"PLAY",0);
        }
//        rt_kprintf("button 1 double click\n");
    break;

    case LONG_PRESS_START:
//        for(j=0;j<num_of_music;j++)
//            rt_kprintf("1%s\n",table[j]);
//        rt_kprintf("button 1 long press start\n");
    break;

    case LONG_PRESS_HOLD:
//        rt_kprintf("button 1 long press hold\n");
    break;
    }
}

static void btn_thread_entry(void* p)
{
    while(1)
    {
        /* 5ms */
        rt_thread_delay(RT_TICK_PER_SECOND/200);

        button_ticks();
    }
}

static int multi_button_wavplayer(void)
{
    rt_thread_t thread = RT_NULL;

    /* Create background ticks thread */
    thread = rt_thread_create("btn", btn_thread_entry, RT_NULL, 512, 10, 10);
    if(thread == RT_NULL)
    {
        return RT_ERROR;
    }
    rt_thread_startup(thread);

    /* low level drive */
    rt_pin_mode  (BUTTON_PIN_0, PIN_MODE_INPUT_PULLUP);
    button_init  (&btn_0, button_read_pin_0, PIN_LOW);
    button_attach(&btn_0, SINGLE_CLICK,     button_0_callback);
    button_attach(&btn_0, DOUBLE_CLICK,     button_0_callback);
    button_attach(&btn_0, LONG_PRESS_START, button_0_callback);
    button_attach(&btn_0, LONG_PRESS_HOLD,  button_0_callback);
    button_start (&btn_0);

    rt_pin_mode  (BUTTON_PIN_1, PIN_MODE_INPUT_PULLUP);
    button_init  (&btn_1, button_read_pin_1, PIN_LOW);
    button_attach(&btn_1, SINGLE_CLICK,     button_1_callback);
    button_attach(&btn_1, DOUBLE_CLICK,     button_1_callback);
    button_attach(&btn_1, LONG_PRESS_START, button_1_callback);
    button_attach(&btn_1, LONG_PRESS_HOLD,  button_1_callback);
    button_start (&btn_1);

    return RT_EOK;
}

//MSH_CMD_EXPORT(multi_button_wavplayer, button wavplayer)
INIT_APP_EXPORT(multi_button_wavplayer);


